package com.example.helloworld;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.widget.SectionIndexer;

import com.example.helloworld.tools.Tools;

public class ItemDataSource {
	
	private SQLiteDatabase db;
	private MySQLliteHelper helper;
	private static final int DB_VERSION = 2;
	
	public ItemDataSource(Context context) {
		helper = new MySQLliteHelper(context);
	}
	
	public void open() throws SQLException {
		db = helper.getWritableDatabase();
	}
	
	public void close() {
		helper.close();
	}
	
	public long createItem(Item item) {
		long insertId = -1;
		ContentValues values = new ContentValues();
		if(item instanceof ItemObject) {
			ItemObject itemObject = (ItemObject)item;
			values.put(Tools.DBTableItems.TITLE, itemObject.getTitle());
			values.put(Tools.DBTableItems.STATUS, itemObject.getStatus());
			values.put(Tools.DBTableItems.PRICE, itemObject.getPrice());
			values.put(Tools.DBTableItems.SECTION, itemObject.getSectionId());
			insertId = db.insert(Tools.DBTableItems.TABLE, null, values);
		} else if(item instanceof ItemSection) {
			ItemSection itemSection = (ItemSection)item;
			values.put(Tools.DBTableSections.TITLE, itemSection.getTitle());
			insertId = db.insert(Tools.DBTableSections.TABLE, null, values);
		}
		return insertId;
	}
	
	public ItemObject getItemOject(int id) {
		ItemObject item = null;
		Cursor cursor = db.query(Tools.DBTableItems.TABLE, Tools.DBTableItems.ALL_FIELDS, Tools.DBTableItems.ID + " = " + id, null, null, null, null);
		if(cursor != null) {
			cursor.moveToFirst();
		}
		item = new ItemObject(cursor.getString(cursor.getColumnIndex(Tools.DBTableItems.TITLE)), 
				cursor.getString(cursor.getColumnIndex(Tools.DBTableItems.STATUS)), 
				cursor.getInt(cursor.getColumnIndex(Tools.DBTableItems.PRICE)), 
				cursor.getLong(cursor.getColumnIndex(Tools.DBTableItems.SECTION)), 
				cursor.getLong(cursor.getColumnIndex(Tools.DBTableItems.ID)));
		return item;
	}
	

	public ItemSection getItemSection(int id) {
		ItemSection item = null;
		Cursor cursor = db.query(Tools.DBTableItems.TABLE, Tools.DBTableSections.ALL_FIELDS, Tools.DBTableSections.ID + " = " + id, null, null, null, null);
		if(cursor != null) {
			cursor.moveToFirst();
		}
		item = new ItemSection(cursor.getString(cursor.getColumnIndex(Tools.DBTableSections.TITLE)),
				cursor.getLong(cursor.getColumnIndex(Tools.DBTableSections.ID)));
		return item;
	}
	
	
	public Cursor getAllItemSectionsCursor() {
		Cursor cursor = db.query(Tools.DBTableSections.TABLE, Tools.DBTableSections.ALL_FIELDS, null, null, null, null, null);
		return cursor;
	}
	
	public Cursor getAllItemObjectsCursor() {
		Cursor cursor = db.query(Tools.DBTableItems.TABLE, Tools.DBTableItems.ALL_FIELDS, null, null, null, null, null);
		return cursor;
	}
	
	public ArrayList<ItemSection> getAllItemSectionsList() {
		List<ItemSection> list = new ArrayList<ItemSection>();
		Cursor cursor = getAllItemSectionsCursor();
		while(cursor.moveToNext()) {
			ItemSection itemToAdd = new ItemSection(cursor.getString(cursor.getColumnIndex(Tools.DBTableSections.TITLE)), 
					cursor.getLong(cursor.getColumnIndex(Tools.DBTableSections.ID)));
			list.add(itemToAdd);
		}
		return (ArrayList<ItemSection>)list;
	}
	
	public ArrayList<ItemObject> getAllItemObjectsList() {
		List<ItemObject> list = new ArrayList<ItemObject>();
		Cursor cursor = getAllItemObjectsCursor();
		while(cursor.moveToNext()) {
			ItemObject itemToAdd = new ItemObject(cursor.getString(cursor.getColumnIndex(Tools.DBTableItems.TITLE)), 
					cursor.getString(cursor.getColumnIndex(Tools.DBTableItems.STATUS)), 
					cursor.getInt(cursor.getColumnIndex(Tools.DBTableItems.PRICE)), 
					cursor.getLong(cursor.getColumnIndex(Tools.DBTableItems.SECTION)), 
					cursor.getLong(cursor.getColumnIndex(Tools.DBTableItems.ID)));
			list.add(itemToAdd);
		}
		
		return (ArrayList<ItemObject>)list;
	}
	
	
	public void updateItemObject(ItemObject item) {
		ContentValues values = new ContentValues();
		values.put(Tools.DBTableItems.TITLE, item.getTitle());
		values.put(Tools.DBTableItems.STATUS, item.getStatus());
		values.put(Tools.DBTableItems.PRICE, item.getPrice());
		
		db.update(Tools.DBTableItems.TABLE, values, Tools.DBTableItems.ID + " =?", new String[] {Long.toString(item.getId())});
	}
	
	public void deleteSection(ItemSection section) {
//TODO
		db.delete(Tools.DBTableSections.TABLE, Tools.DBTableSections.ID + " = " + section.getId() , null);
		db.delete(Tools.DBTableItems.TABLE, Tools.DBTableItems.TABLE + "." + Tools.DBTableItems.SECTION + " = " + section.getId(), null);
	}
	
	public void deleteItem(ItemObject object) {
		//TODO
		
		db.delete(Tools.DBTableItems.TABLE, Tools.DBTableItems.ID + " = " + object.getId() , null);
		
	} 
	
}
































